﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.IO;
using System.Web.Core;

namespace Yuanlin.Business
{
    public class CompanyProjectPerformanceEditControl : YuanlinUserControl,
        IReplyConverter<CompanyProjectPerformanceReply, GetOperation>
    {
        protected ValuedTextBox titleValuedTextBox;
        protected ValuedEditor descriptionValuedEditor;
        protected Image viewImage;

        protected HtmlInputFile attachmentData;
        protected IButton SaveButton;
        protected IButton BackButton;
        private CompanyProjectPerformance companyProjectPerformance;

        public override void GetRequests(List<BaseRequestBinder> requestBinders)
        {
            requestBinders.Add(RequestHelper.BuildGetCompanyProjectPerformanceRequest(this));
        }
        protected override void OnLoad(EventArgs e)
        {
            SaveButton.Click += new EventHandler(SaveButton_Click);
            BackButton.Click += new EventHandler(BackButton_Click);
        }

        private void Bind()
        {
            if (companyProjectPerformance != null)
            {
                this.titleValuedTextBox.Value = companyProjectPerformance.Title.Value;
                this.descriptionValuedEditor.Value = companyProjectPerformance.Description.Value;
                viewImage.Attributes.Add("onclick", "javascript:window.open('" + viewImage.ImageUrl + "')");
                viewImage.ImageUrl = SiteUrls.Instance().GetAttachmentFullVirtualPathUrl(companyProjectPerformance);
                viewImage.ToolTip = companyProjectPerformance.Title.Value;
            }
        }

        public void BackButton_Click(object sender, EventArgs e)
        {
            PageRedirect();
        }

        public void SaveButton_Click(object sender, EventArgs e)
        {
            if (companyProjectPerformance != null)
            {
                companyProjectPerformance.Title.Value =  CensorWordManager.CensorWord(this.titleValuedTextBox.Value);
                companyProjectPerformance.Description.Value =  CensorWordManager.CensorWord(this.descriptionValuedEditor.Value);

                if(BusinessManager.UpdateCompanyProjectPerformance(companyProjectPerformance) ==-2)
                {
                    Globals.ShowMessage(this.Page, "修改失败", "此信息已经存在！");
                    return;
                }

                if (SiteUrls.Instance().IsAttachmentValid(attachmentData))
                {
                    companyProjectPerformance.AttachmentFileName.Value = SiteUrls.Instance().GetPostedFileName(attachmentData);
                    BusinessManager.UpdateCompanyProjectPerformance(companyProjectPerformance);
                    SiteUrls.Instance().SaveFile(SiteUrls.Instance().GetFile(companyProjectPerformance), attachmentData);
                }
            }
            PageRedirect();
        }
        private void PageRedirect()
        {
            Page.Response.Redirect(SiteUrls.Instance().GetCompanyProjectPerformanceListUrl());
        }
        #region IReplyConverter<CompanyProjectPerformanceReply,GetOperation> Members

        public void ConvertReply(CompanyProjectPerformanceReply reply, GetOperation operation)
        {
            if (reply != null)
            {
                companyProjectPerformance = reply.Entity as CompanyProjectPerformance;

                if (!Page.IsPostBack)
                {
                    Bind();
                }
            }
        }

        #endregion
    }
}